﻿using NLog;

namespace ArtheaEngine
{
    public partial class Area
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        public bool Equals(Area obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return obj._AreaID == _AreaID;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (Area)) return false;
            return Equals((Area) obj);
        }

        public override int GetHashCode()
        {
            return _AreaID.GetHashCode();
        }

        public void Update()
        {
            Object lastObj = null;
            NonPlayer lastChar = null;

            foreach (Reset r in Resets)
            {
                if (r is CharacterReset)
                {
                    var cr = r as CharacterReset;

                    cr.NonPlayer.Room = cr.Room;
                    lastChar = (NonPlayer) cr.NonPlayer;
                    World.Characters.Add(cr.NonPlayer);
                    cr.Room.Characters.Add(cr.NonPlayer);
                }
                else if (r is ObjectReset)
                {
                    var or = r as ObjectReset;

                    or.Object.Room = or.Room;
                    lastObj = or.Object;
                    World.Objects.Add(or.Object);
                    or.Room.Objects.Add(or.Object);
                }
                else if (r is EquipmentReset)
                {
                    var er = r as EquipmentReset;

                    if (lastChar == null)
                    {
                        log.Error("Equipment reset with no character.");
                    }
                    else
                    {
                        lastChar.Objects.Add(er.Object);
                    }
                }
                else if (r is ContainerReset)
                {
                    var cr = r as ContainerReset;

                    if (lastObj == null)
                    {
                        log.Error("Container reset with no object");
                    }
                    else
                    {
                        lastObj.Contents.Add(new ObjectContent(cr.Object, lastObj));
                    }
                }
            }
        }
    }
}